package metier;

import java.io.Serializable;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import webEquipeRechercheGwt.client.entityMirror.PersonMirror;
import webEquipeRechercheGwt.client.entityMirror.SeminarMirror;

import metier.Person;

@Entity
@Table(name="SEMINAR")
@NamedQueries(
		@NamedQuery(name="getSeminars",
				query="select s from Seminar s " +
				"order by s.title")
			)
public class Seminar implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8242545401970777555L;

	private int id;
	private String title;
	private String abstrac;
	private String place;
	private Date dateFrom;
	private Date dateUntil;
	private String url4Slides;
	private Collection<Person> speakers;
	private SeminarMirror loaded;
	
	@Id
	@GeneratedValue
	public int getId(){
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getAbstrac() {
		return abstrac;
	}
	public void setAbstrac(String abstrac) {
		this.abstrac = abstrac;
	}
	public String getPlace() {
		return place;
	}
	public void setPlace(String place) {
		this.place = place;
	}
	public Date getDateFrom() {
		return dateFrom;
	}
	public void setDateFrom(Date dateFrom) {
		this.dateFrom = dateFrom;
	}
	public Date getDateUntil() {
		return dateUntil;
	}
	public void setDateUntil(Date dateUntil) {
		this.dateUntil = dateUntil;
	}
	public String getUrl4Slides() {
		return url4Slides;
	}
	public void setUrl4Slides(String url4Slides) {
		this.url4Slides = url4Slides;
	}
	@ManyToMany(mappedBy="seminars")
	public Collection<Person> getSpeakers() {
		return speakers;
	}
	public void setSpeakers(Collection<Person> speakers) {
		this.speakers = speakers;
	}
	public void addSpeaker(Person p) {
		speakers.add(p);
	}
	public void fullLoad(){
		Iterator<Person> it = speakers.iterator();
		while(it.hasNext()){
			Person p = it.next();
			p.toString();
		}
	}
	public SeminarMirror getMirror() {
		if(loaded==null){
			loaded=new SeminarMirror();
			loaded.setId(id);
			loaded.setTitle(title);
			loaded.setAbstrac(abstrac);
			loaded.setPlace(place);
			loaded.setDateFrom(dateFrom);
			loaded.setDateUntil(dateUntil);
			loaded.setUrl4Slides(url4Slides);
			Collection<PersonMirror> speakerMirrors = new ArrayList<PersonMirror>();
			for(Person p : speakers){
				speakerMirrors.add(p.getMirror());
			}
			loaded.setSpeakers(speakerMirrors);
		}
		return loaded;
	}
	
}
